import base


class PluralityVoting(base.AbstractVotingMethod):
  """Implements the plurality (first past the post) voting rule."""

  def __init__(self):
    pass

  def name(self) -> str:
    return "plurality"

  def run_election(self, profile: base.PreferenceProfile) -> base.RankOutcome:
    assert self.is_valid_profile(profile)
    tally = {}
    for alternative in profile.alternatives:
      tally[alternative] = 0
    for vote in profile.votes:
      tally[vote.vote[0]] += vote.weight
    sorted_tally = sorted(tally.items(), key=lambda item: item[1], reverse=True)
    outcome = base.RankOutcome()
    outcome.unpack_from(sorted_tally)
    return outcome
